<template>
    <div>
    <!--羔羊断奶-查询 -->
    <el-form :inline="true" :model="form" class="demo-form-inline">        
        &nbsp;&nbsp;&nbsp;&nbsp;<el-form-item label="断奶日期">
            <el-date-picker v-model="form.sDate" type="date" style="width: 130px;"  value-format="YYYY-MM-DD" clearable />-
            <el-date-picker v-model="form.eDate" type="date" style="width: 130px;" value-format="YYYY-MM-DD" clearable />
        </el-form-item> &nbsp;&nbsp;
        <el-form-item label="羔羊耳号">
            <el-input v-model="form.LambOverbit" clearable />
        </el-form-item>
        <el-form-item label="断奶日龄">
            <el-input v-model="form.SWeaningDay" clearable style="width: 130px;"/>-
            <el-input v-model="form.EWeaningDay" clearable style="width: 130px;"/>
        </el-form-item>
        <el-form-item>
            <el-button type="danger" @click="GetLambInfo">查询</el-button>
            <el-button>重置</el-button>
        </el-form-item>
        <br/>
        <el-form-item label="断奶重(公斤)">
            <el-input v-model="form.SWeaningWeight" clearable style="width: 130px;" />-
            <el-input v-model="form.EWeaningWeight" clearable style="width: 130px;" />
        </el-form-item>
        <el-form-item label="哺乳种母耳号">
            <el-input v-model="form.MomEarbugles" clearable />
        </el-form-item>        
    </el-form>
    <el-divider />
      <div>
          <el-button type="danger" size="large" @click="OpenVisible" :icon="Plus">新增</el-button>
          <el-button type="danger" size="large" @click="BatchDel()" :icon="Delete">删除</el-button>
      </div>      
      <br>
      <div>
          <div v-if="TagBool">
              <el-tag style="width:1920px;color: black;height: 50px;" type="danger" :iocn="Delete">
                  <spna style="font-size: 15px">
                      <Warning style="width: 1em; height: 1em; margin-right: 8px" color="red" />
                      <span style="margin-right: 1800px">已选择 {{ TagCount }} 项</span>
                  </spna>
              </el-tag>
          </div>
          <div v-else>
              <el-tag style="width:1920px;color: black;height: 50px;" type="success" :iocn="Delete">
                  <spna style="font-size: 15px">
                      <SuccessFilled style="width: 1em; height: 1em; margin-right: 8px" color="green" />
                      <span style="margin-right: 1800px">已选择 {{ TagCount }} 项</span>
                  </spna>
              </el-tag>
          </div>
      </div> 
      <br />
      <!--死亡管理-列表 -->
      <el-table :data="LambWeaningList" stripe style="width: 100%" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center" />
        <el-table-column label="序号" :fixed="true" width="110px" >
        <template v-slot="a">
            <span>{{ a.$index + (page.pageIndex - 1) * page.pageSize + 1 }}</span>
        </template>
        </el-table-column>
        <el-table-column prop="LambOverbit" label="羔羊耳号" width="110px" />
        <el-table-column prop="WeaningDay" label="断奶日龄" width="110px" />
        <el-table-column prop="WeaningWeight" label="断奶重(公斤)" width="110px" />
        <el-table-column prop="MomEarbugles" label="哺乳种母耳号" width="110px" />
        <el-table-column prop="AtPresentParity" label="胎次" width="110px" />
        <el-table-column prop="FrontDorm" label="断奶栋舍" width="110px" />
        <el-table-column prop="FrontColumns" label="断奶栏位" width="110px" />
        <el-table-column prop="QueenDorm" label="转入栋舍" width="110px" />
        <el-table-column prop="QueenColumns" label="转入栏位" width="110px" />
        <el-table-column prop="WeaningDate" label="断奶日期↓" width="110px">
            <template v-slot="scope">
            <span>{{
            scope.row.WeaningDate
            ? scope.row.WeaningDate.replace("T", " ").substring(0, 10)
            : null
            }}</span>
            </template>
        </el-table-column>>
        <el-table-column prop="CreateBy" label="创建人" width="110px" />
        <el-table-column prop="CreateDate" label="创建时间" width="110px">
          <template v-slot="scope">
            <span>{{
              scope.row.CreateDate
                ? scope.row.CreateDate.replace("T", " ").substring(0, 19)
                : null
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="UpdateBy" label="修改人" width="110px" />
        <el-table-column prop="UpdateDate" label="修改时间" width="110px">
          <template v-slot="scope">
            <span>{{
              scope.row.UpdateDate
                ? scope.row.UpdateDate.replace("T", " ").substring(0, 19)
                : null
            }}</span>
          </template>
        </el-table-column>
        <el-table-column  fixed="right" width="110px" label="操作">
          <template v-slot="scope" #default>
            <el-link type="primary" @click="Del(scope.row.WeaningId)">删除</el-link>&nbsp; 
            <el-link type="primary" @click="UpdInfo(scope.row.WeaningId)">修改</el-link>
          </template>
        </el-table-column>
      </el-table>
       <!-- 分页 -->
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="page.pageIndex"
          v-model:page-size="page.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <!-- 新增行 -->
      <el-dialog v-model="dialogTableVisible" title="羔羊断奶" width="90%" style="height: 100%;" :before-close="NewUpdate">
        <div>
          断奶日期:<el-date-picker v-model="LambInfo.WeaningDate" type="date" value-format="YYYY-MM-DD" />
          &nbsp;&nbsp;&nbsp;&nbsp;
          单据号:<el-input v-model="LambInfo.DocumentCord" style="width: 220px;" ></el-input>
          <br><br>
          <div>
              <el-button type="danger" size="default" @click="addLamb" :icon="Plus"
                  v-if="LambInfo.WeaningId == 0">新增</el-button>
              <el-button type="danger" size="default" @click="addLamb" :icon="DocumentChecked"
                  v-else>保存</el-button>
                <el-button type="danger" disabled v-show="LambInfo.WeaningId == 0">保存</el-button> 
                <el-button type="danger" disabled v-show="LambInfo.WeaningId == 0">撤销</el-button> 
                <el-button type="danger" disabled v-show="LambInfo.WeaningId == 0">删除</el-button> 
          </div>
         <br><br>
          <el-table :data="rowList" style="width: 90%" border>
            <el-table-column prop="Earbugles" label="耳号">
                <span>
                    <el-select v-model="LambInfo.LambOverbit"  @change="AddEar" clearable>      
                      <el-option
                        v-for="item in ear"
                        :key="item.id"
                        :label="item.name"
                        :value="item.name"
                      />
                    </el-select>
                </span>
            </el-table-column>
            <el-table-column prop="Sex" label="性别 ">
                <span>
                    <el-select  el-select v-model="LambInfo.Sex" clearable>
                      <el-option v-for="item in SexList" :key="item.value" :label="item.name" :value="item.value" />
                    </el-select> 
                </span>
            </el-table-column>
            <el-table-column prop="FrontDorm" label="当前栋舍">
                <span>
                    <el-select v-model="LambInfo.FrontDorm" placeholder="请选择" clearable
                        @change="GetField1()">
                        <el-option v-for="item in Cottage" :key="item.Id" :label="item.Name"
                            :value="item.Name" />
                    </el-select>
                </span>
            </el-table-column>
            <el-table-column prop="FrontColumns" label="当前栏位">
                <span>
                    <el-select v-model="LambInfo.FrontColumns" placeholder="请选择" clearable>
                        <el-option v-for="item in Field" :key="item.Id" :label="item.Name" :value="item.Name" />
                    </el-select>
                </span>
            </el-table-column>
            <el-table-column prop="QueenDorm" label="转入栋舍">
                <span>
                    <el-select v-model="LambInfo.QueenDorm" placeholder="请选择" clearable
                        @change="GetField2()">
                        <el-option v-for="item in Cottage" :key="item.Id" :label="item.Name"
                            :value="item.Name" />
                    </el-select>
                </span>
            </el-table-column>
            <el-table-column prop="QueenColumns" label="转入栏位">
                <span>
                    <el-select v-model="LambInfo.QueenColumns" placeholder="请选择" clearable>
                        <el-option v-for="item in Field" :key="item.Id" :label="item.Name" :value="item.Name" />
                    </el-select>
                </span>
            </el-table-column>
            <el-table-column prop="WeaningWeight" label="断奶重">
                <span>
                    <el-input v-model="LambInfo.WeaningWeight" clearable  />
                </span>
            </el-table-column>           
          </el-table>
        </div> 
      </el-dialog>
    </div>
</template>
<script setup lang="ts">
import http from '@/http'
import { ref, reactive, inject, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Plus, Delete,Stamp, DocumentChecked, Warning, SuccessFilled } from '@element-plus/icons-vue'
import { initCustomFormatter } from 'vue';

//计算日期之间的天数
const DateDiffer = (Date_end: any) => {
    //date1结束时间
    let date1 = new Date(Date_end);
    //date2当前时间
    let date2 = new Date();
    const diff = date2.getTime() - date1.getTime(); //目标时间减去当前时间
    const diffDate = diff / (24 * 3600 * 1000 ) -1; //计算当前时间与结束时间之间相差天数
    return Math.ceil(diffDate);
};
//调用添加层
const OpenVisible = () => {
  addFormInfo();  
  LambInfo.WeaningId = 0;
  LambInfo.WeaningDate = "";
  LambInfo.DocumentCord = "";
  dialogTableVisible.value = true;
}
//羊羔断奶表单
const LambInfo = reactive({
    WeaningId: 0,
    WeaningDate:"",
    LambOverbit:"",
    MomEarbugles:"",
    AtPresentParity:"",
    WeaningDay:0,
    Sex: 0,
    DocumentCord:"",
    FrontDorm:"",
    FrontColumns:"",
    QueenDorm:"",
    QueenColumns:"",
    WeaningWeight:"",
    CreateBy:"",
    CreateDate:"",
    UpdateBy:"",
    UpdateDate:"",
    IsDel: false    
})
const NewUpdate = () => {
 rowList.value = [];
 dialogTableVisible.value = false;
}
//添加弹出层初始值
const dialogTableVisible = ref(false);
const rowList = ref([]);
//新增行
const rowLength = ref(0);
const addFormInfo = () => {
    LambInfo.WeaningDate = "";
    LambInfo.LambOverbit = "";
    LambInfo.Sex = 0||null;
    LambInfo.FrontDorm = "";
    LambInfo.FrontColumns = "";
    LambInfo.QueenDorm = "";
    LambInfo.QueenColumns = "";    
    LambInfo.WeaningWeight = "";
    rowList.value.push(newRow);
}
//新增行表单
var newRow = {
    LambOverbit: "",
    Sex: 0,  
    FrontDorm: "",
    FrontColumns: "",
    QueenDorm: "",
    QueenColumns: "",
    WeaningWeight: "",
}
//耳号与档案的值
const AddEar = () => {
  http.get("MYroute/api/SheepFile/SheepFileConditionarprogeny?card="+  LambInfo.LambOverbit+"&type=3")
    .then(res => {
    LambInfo.Sex = res.data.sex;
    LambInfo.FrontDorm = res.data.cottage;
    LambInfo.FrontColumns = res.data.field;
    LambInfo.FrontColumns = res.data.field;
    LambInfo.MomEarbugles = res.data.femaleHorn;
  })
}
//添加修改羔羊断奶
const addLamb = () => {
    if (LambInfo.WeaningDate == "") {
    
      ElMessage({
          type: "error",
          message: "断奶日期不能为空",
          duration: 2000
      })
    return;
    }
    if (LambInfo.DocumentCord == "") {    
    ElMessage({
        type: "error",
        message: "单据号不能为空",
        duration: 2000
    })
    return;
    }
    if (LambInfo.LambOverbit == "") {
    
      ElMessage({
          type: "error",
          message: "羔羊耳号不能为空",
          duration: 2000
      })
    return;
    }
    if (LambInfo.QueenColumns == null) {
    
      ElMessage({
          type: "error",
          message: "请选择转出栋舍",
          duration: 2000
      })
      return;
    }
    if (LambInfo.QueenDorm == null) {
    
      ElMessage({
          type: "error",
          message: "请选择转出栏位",
          duration: 2000
      })
    return;
    }
    if (LambInfo.WeaningWeight == "") {    
      ElMessage({
          type: "error",
          message: "断奶重不能为空",
          duration: 2000
      })
        return;
    }
    if (LambInfo.WeaningId == 0) {
        LambInfo.WeaningDay = DateDiffer(new Date(LambInfo.WeaningDate));  
         http.post("first/api/LambWeaning/AddLambWeaning", LambInfo)
        .then(res => {
          if (res.data > 0) {
              ElMessage({
                  type: "success",
                  message: "保存成功",
                  duration: 2000
              })             
              http.get("MYroute/api/SheepFile/SheepFileConditionFinds?card=" + LambInfo.LambOverbit).then(res => {
                  var form = res.data;
                  form.field = LambInfo.QueenColumns;
                  form.cottage = LambInfo.QueenDorm;
                  http.put("Mroute/api/SheepFile/SheepFileConditionUpdate", form)
              })
              dialogTableVisible.value = false;
              NewUpdate();
              GetLambInfo();
          } else if (res.data == -1) {
              ElMessage({
                  type: "error",
                  message: "保存失败，耳号不能重复",
                  duration: 2000
              })
          }
          else {
              ElMessage({
                  type: "error",
                  message: "保存失败",
                  duration: 2000
              })
          }
        })
    }
    else {
        LambInfo.WeaningDay = DateDiffer(new Date(LambInfo.WeaningDate));  
         http.post("first/api/LambWeaning/UpdLambWeaning", LambInfo)
        .then(res => {
          if (res.data > 0) {
              ElMessage({
                  type: "success",
                  message: "保存成功",
                  duration: 2000
              })
             
              http.get("MYroute/api/SheepFile/SheepFileConditionFinds?card=" + LambInfo.LambOverbit).then(res => {
                  var form = res.data;
                  form.field = LambInfo.QueenColumns;
                  form.cottage = LambInfo.QueenDorm;
                  http.put("Mroute/api/SheepFile/SheepFileConditionUpdate", form)
              })
              dialogTableVisible.value = false;
              NewUpdate();
              GetLambInfo();
          } else if (res.data == -1) {
              ElMessage({
                  type: "error",
                  message: "保存失败，耳号不能重复",
                  duration: 2000
              })
          }
          else {
              ElMessage({
                  type: "error",
                  message: "保存失败",
                  duration: 2000
              })
          }
        })
    }
}
//点击修改事件弹出层
const UpdInfo = (WeaningId: number | any) => {
  addFormInfo();
  if (WeaningId != 0) {
    http.get("srout/api/LambWeaning/EditLambWeaning?WeaningId="+WeaningId)
      .then(res => {
        LambInfo.DocumentCord = res.data.DocumentCord;
        LambInfo.WeaningId = res.data.WeaningId;
        LambInfo.WeaningDate = res.data.WeaningDate;
        LambInfo.LambOverbit = res.data.LambOverbit;
        LambInfo.MomEarbugles = res.data.MomEarbugles;
        LambInfo.WeaningDay = res.data.WeaningDay;      
        LambInfo.Sex = res.data.Sex;
        LambInfo.FrontDorm = res.data.FrontDorm;
        LambInfo.FrontColumns = res.data.FrontColumns;
        LambInfo.QueenDorm = res.data.QueenDorm;
        LambInfo.QueenColumns = res.data.QueenColumns;
        LambInfo.WeaningWeight = res.data.WeaningWeight;
        LambInfo.UpdateBy = res.data.UpdateBy;
        LambInfo.UpdateDate = res.data.UpdateDate;
        LambInfo.CreateBy = res.data.CreateBy;
        LambInfo.CreateDate = res.data.CreateDate;        
        GetField1();
        GetField2();
      })
      dialogTableVisible.value = true;
  }
}
//羔羊断奶列表
const LambWeaningList = ref([]);
//查询form
const form = reactive({  
  LambOverbit: "",
  MomEarbugles: "",
  eDate: "",
  sDate: "",
  SWeaningDay: "",
  EWeaningDay: "",
  SWeaningWeight: "",
  EWeaningWeight: "",
});
//羔羊断奶显示
const GetLambInfo = () => {
    http.get("srout/api/LambWeaning/GetLambWeaning", {
        params: {
            pageIndex: page.pageIndex,
            pageSize: page.pageSize,
            Edate: form.eDate,
            Sdate: form.sDate,
            LambOverbit: form.LambOverbit || null,
            MomEarbugles: form.MomEarbugles || null,
            SWeaningDay: form.SWeaningDay || null,
            EWeaningDay: form.EWeaningDay || null,
            SWeaningWeight: form.SWeaningWeight || null,
            EWeaningWeight: form.EWeaningWeight || null,
        }
    })
    .then(res => {
        LambWeaningList.value = res.data.datas;
        page.totalCount = res.data.totalCount;
        page.pageCount = res.data.pageCount;
    })
}
//分页属性
const page = {
  pageIndex: 1,
  pageSize: 10,
  pageCount: 0,
  totalCount: 0,
};
//总页数
const handleSizeChange = (val: number) => {
  page.pageSize = val;
  GetLambInfo();
};
//总条数
const handleCurrentChange = (val: number) => {
  page.pageIndex = val;
  GetLambInfo();
};
//获取选中的数组
const refs = ref([
{WeaningId:""}
])
//选中数据
let ids = "";
const TagBool = ref(true);
const TagCount = ref(0);
const handleSelectionChange = (val: refs | any) => {
    refs.value = val;
    ids = "";
    val.forEach((x: { WeaningId: string; }) => {
        ids += x.WeaningId + ",";
    })
    ids = ids.substring(0, ids.length - 1);
    TagCount.value = ids.split(',').length;
    if (ids == "") {
        TagCount.value = 0;
    }
    if (TagCount.value == 0) {
        TagBool.value = true;
    }
    else {
        TagBool.value = false;
    }
}
//单个删除
const Del = (WeaningId: any) => {
  ElMessageBox.confirm("确定要此数据删除吗?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      http
        .delete("first/api/LambWeaning/DelLambWeaning?WeaningId=" + WeaningId)
        .then((res) => {
          if (res.data > 0) {
            ElMessage({
              type: "success",
              message: "信息删除成功",
            });
            GetLambInfo();
          } else {
            ElMessage({
              type: "error",
              message: "信息删除失败",
            });
          }
        });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "Delete canceled",
      });
    });
};
//批量删除
const BatchDel = () => {
  let idss = refs.value.map(x => x.WeaningId);
  if (refs.value.length == 0) {
    ElMessage.error("至少选择一项");
    return;
  }
  ElMessageBox.confirm("确定要此数据删除吗?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      http
        .post("first/api/LambWeaning/BatchLambWeaning?Array=" + idss)
        .then((res) => {
          if (res.data > 0) {
            ElMessage({
              type: "success",
              message: "信息批量删除成功",
            });
            GetLambInfo();
          } else {
            ElMessage({
              type: "error",
              message: "信息批量删除失败",
            });
          }
        });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "Delete canceled",
      });
    });
}
//耳号
const ear = ref()
//性别集合
const SexList = ref([
  {
    value: "",
    name: "",
  },
]);
//栋舍集合
const Cottage = ref([]);
//栋舍下拉方法
const GetCooter = () => {
    http.get("OrganizationRead/api/Cottage/HandleGetCot").then(res => {
        Cottage.value = res.data;
    })
}
//栏位集合
const Field = ref([]);
//当前栏位下拉方法
const GetField1 = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + LambInfo.FrontDorm).then(res => {
        Field.value = res.data;
    })
}
//转入栏位下拉方法
const GetField2 = () => {
    http.get("OrganizationRead/api/Field/HandleGetSheepFarm?Id=" + LambInfo.QueenDorm).then(res => {
        Field.value = res.data;
    })
}
//集合
const GetDeathCauseList = () => {
    http.get("MYroute/api/SheepFile/SheepFileConditionEard").then(res=>{
        ear.value=res.data;
    })    
    http.get("srout/api/Breeding/GetSex").then((res) => {
      SexList.value = res.data;
    });
   
};
onMounted(() => {
    GetLambInfo();
    GetCooter();
    GetDeathCauseList();
})
</script>
<style>
    
</style>